<?php


namespace app\api\model;


use app\common\library\IDMan;
use app\common\library\StatModel;
use app\common\model\BaseModel;
use common;
use think\Db;

class CityAnalyzeModel extends BaseModel
{
    protected $name = 'city_report_log';

    function analyze($city_id,$city_name){

        $city_bike = (new BikeModel())->analyze($city_id);

        $now = date('Y-m-d');
        $end_timestamp = strtotime($now);

        $date_time = date('Y-m-d', $end_timestamp - 3600 * 24);

        $order_num = (new StatModel())->user_ride_count($city_id,$date_time);

        $bike_count = (new StatModel())->bike_count_get($city_id,$date_time);

        $eff = 0;
        if ($bike_count >0) {
            $eff =  round($order_num/$bike_count, 1);
        }

        $ID = IDMan::city_order_id($city_id);
        $arr=[
            'id' =>$ID,
            'day_time' => $date_time,
            'city_id' => $city_id,
            'city_name' => $city_name,
            'city_wealth' => '无',           //天气
            'city_order_num' => $order_num, //订单数
            'city_bike_eff' => $eff,         //系统车效
            'city_bike_teff' => 0,          //实际车效
            'bike_count' => $city_bike['total'],    //车辆数量
            'bike_col'  => 0,   //收车
            'bike_fault' => 0,  //事故车
            'bike_unhealth' => $city_bike['fault_count'],   //故障车
            'bike_off' => $city_bike['offline_count'],      //离线车辆
            'repark_num' => 0,      //有效挪车
            'battery_num' => 0,     //有效换电
            'dispatch_num' => 0,    //派单
            'violation_num' => 0,   //举报
            'repair_num' => 0,      //维修
        ];


        self::create($arr);


        return $ID;
    }

    function analyze_person($city_id,$log_id){
        $now = date('Y-m-d');
        $end_timestamp = strtotime($now);

        $date_time = date('Y-m-d', $end_timestamp - 3600 * 24);

        $cd =[
            'state' => 0,
        ];
        $list = (new StaffModel())->query_list($city_id, $cd, 0, 1000);

        $staff_list = [];
        foreach ($list as $val){
            $staff_info =[
                'id' => $val['id'],
                'name' => $val['name'],
                'phone' => $val['phone'],
                'repark_num' => 0,             //挪车
                'battery_num' => 0,           //电池
                'dispatch_num' => 0,         //派发
                'violation_num' => 0,       //违规举报
                'repair_num' =>0,           //维修
            ];

            $staff_list[$val['id']] = $staff_info;
        }

        $condition =[
            'pos' => 0,
            'limit' => 5000,
            'start_time' => $date_time . ' 00:00:00',
            'end_time' => $date_time . ' 23:59:59',
        ];


        $repark_list = (new ReparkOrderModel())->analyze($city_id,$condition,2,1);
        $battery_list = (new BatteryChangeOrderModel())->analyze($city_id,$condition,2);
        $dispatch_list = (new BikeDispatchModel())->analyze($city_id,$condition,2);
        $violation_list = (new UserRideViolationOrderModel())->analyze($city_id,$condition);
        $repair_list = (new BikeRepairModel())->analyze($city_id,$condition);


        $repark_num = 0;      //有效挪车
        $battery_num = 0;     //有效换电
        $dispatch_num = 0;    //派单
        $violation_num = 0;   //举报
        $repair_num = 0;      //维修

        foreach ($repark_list as $key=>$value) {
            if (isset($staff_list[$key])){
                $staff_list[$key]['repark_num'] = $value;
                $repark_num +=  $value;
            }

        }

        foreach ($battery_list as $key=>$value) {
            if (isset($staff_list[$key])){
                $staff_list[$key]['battery_num'] = $value;
                $battery_num += $value;
            }

        }

        foreach ($dispatch_list as $key=>$value) {
            if (isset($staff_list[$key])){
                $staff_list[$key]['dispatch_num'] = $value;
                $dispatch_num += $value;
            }

        }

        foreach ($violation_list as $key=>$value) {
            if (isset($staff_list[$key])){
                $staff_list[$key]['violation_num'] = $value;
                $violation_num += $value;
            }

        }

        foreach ($repair_list as $key=>$value) {
            if (isset($staff_list[$key])){
                $staff_list[$key]['repair_num'] = $value;
                $repair_num += $value;
            }
        }

        //更新log表
        $data=[
            'repark_num' => $repark_num,      //有效挪车
            'battery_num' => $battery_num,     //有效换电
            'dispatch_num' => $dispatch_num,    //派单
            'violation_num' => $violation_num,   //举报
            'repair_num' => $repair_num,      //维修
        ];

        $where =[
            'id' => $log_id,
        ];

        //hilog::info($data,$where);
        self::update($data,$where);

        //插入表
        foreach ($staff_list as $key=>$val){
            if ($val['repark_num'] >0 || $val['battery_num'] >0 || $val['dispatch_num']>0
                || $val['violation_num'] >0 || $val['repair_num'] >0) {

                $tmp_val =[
                    'log_id' => $log_id,
                    'city_id' => $city_id,
                    'day_time' => $date_time,
                    'staff_id' => $val['id'],
                    'staff_name' => $val['name'],
                    'staff_phone' => $val['phone'],
                    'repark_num' => $val['repark_num'],             //挪车
                    'battery_num' => $val['battery_num'],           //电池
                    'dispatch_num' => $val['dispatch_num'],         //派发
                    'violation_num' => $val['violation_num'],       //违规举报
                    'repair_num' => $val['repair_num'],             //维修
                ];

                self::create($tmp_val);
            }
        }

        //(new StatModel())->daily_person_report($city_id,$date_time,$staff_list);
    }


    function get_city_person_daily_report($city_id,$log_id,$pos,$limit){
        $selectlist =[
            'id' => 'int',
            'log_id' => 'string',
            'city_id' => 'string',
            'day_time' => 'string',
            'staff_id' => 'string',
            'staff_name' => 'string',
            'staff_phone' => 'string',
            'repark_num' => 'int',             //挪车
            'battery_num' => 'int',           //电池
            'dispatch_num' =>'int',         //派发
            'violation_num' => 'int',       //违规举报
            'repair_num' => 'int',             //维修
        ];

        $sfs = common::array_keys($selectlist);
        $beans = [
            'log_id'=>$log_id,
        ];

        $rows = Db::table('tb_city_staff_report_log')->where($beans)->field($sfs)->limit($pos,$limit)->select();
        $l = [];
        foreach ($rows as $row) {
            $l[] = common::sql_row($selectlist, $row);
        }
        return $l;
    }


    function query_info($city_id,$day_time)
    {
        $selectlist = [
            'id' => 'string',
            'day_time' => 'string',
            'city_id' => 'string',
            'city_name' => 'string',
            'city_wealth' => 'string',
            'city_order_num' => 'int',
            'city_bike_eff' => 'float',
            'city_bike_teff' => 'float',
            'bike_count' => 'int',
            'bike_col' => 'int',
            'bike_fault' => 'int',
            'bike_unhealth' => 'int',
            'bike_off' => 'int',
            'repark_num' => 'int',
            'battery_num' => 'int',
            'dispatch_num' => 'int',
            'violation_num' => 'int',
            'repair_num' => 'int',
            'url' => 'string',
            'remark' => 'string',
        ];

        $sfs = common::array_keys($selectlist);
        $bt=[
            'city_id'=>$city_id,
            'day_time'=>$day_time,
        ];


        return $this->where($bt)->field($sfs)->find();
    }

    function query_list($city_id,$start_time,$end_time,$pos,$limit)
    {
        $selectlist = [
            'id' => 'string',
            'day_time' => 'string',
            'city_id' => 'string',
            'city_name' => 'string',
            'city_wealth' => 'string',
            'city_order_num' => 'int',
            'city_bike_eff' => 'float',
            'city_bike_teff' => 'float',
            'bike_count' => 'int',
            'bike_col' => 'int',
            'bike_fault' => 'int',
            'bike_unhealth' => 'int',
            'bike_off' => 'int',
            'repark_num' => 'int',
            'battery_num' => 'int',
            'dispatch_num' => 'int',
            'violation_num' => 'int',
            'repair_num' => 'int',
            'url' => 'string',
            'remark' => 'string',
        ];

        $sfs = common::array_keys($selectlist);
        $st = "day_time >= ? and day_time <= ?";
        $beans = [
            $start_time,
            $end_time
        ];

        if (!empty($city_id)){
            $st = $st . " and city_id = ?";
            $beans[] = $city_id;
        }

        $rows = $this->where($st,$beans)->field($sfs)->order('day_time')->limit($pos,$limit)->select();
        $l = [];
        foreach ($rows as $row) {
            $l[] = common::sql_row($selectlist, $row);
        }
        return $l;
    }



    function set_daily_data($condtion){
        $day_time =$condtion['day_time'];
        $city_id = $condtion['city_id'];
        $ret = $this->query_info($city_id,$day_time);

        if ($ret)
        {
            $info=[
                'city_wealth'=> $condtion['city_wealth'],
                'bike_col' => $condtion['bike_col'],
                'bike_fault' => $condtion['bike_fault'],
            ];

            $true_bike = $ret['bike_count'] - $info['bike_col'] - $info['bike_fault'] - $ret['bike_unhealth'];

            if ($true_bike <=0)
                $true_bike = 1;

            $info['city_bike_teff'] =  round($ret['city_order_num']/$true_bike, 1);

            if (isset($condtion['url'])){
                $info['url'] = $condtion['url'];
            }

            if (isset($condtion['remark'])){
                $info['remark'] = $condtion['remark'];
            }


            $where=[
                'id' => $ret['id'],
            ];
            self::update($info,$where);
            //(new StatModel())->daily_report($city_id,$day_time,$info);
        }
    }

}